package com.cloudcc.mobile.db;

import android.content.Context;

import com.cloudcc.mobile.entity.chat.Task;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
/**
 * 任务数据库
 * @author Administrator
 *
 */
public class TaskDB {
	private Context context;
	private DbUtils db;
	public TaskDB(Context context,String dbName){
		db=DbUtils.create(context, dbName);
		this.context=context;
	}
	/**
	 * 保存数据库
	 * @param cls
	 * @throws DbException
	 */
	public  void saveTask(Task entity) throws DbException{
		db.save(entity);
	}
	
	/**
	 * 删除所有消息
	 * @param <T>
	 * @throws DbException 
	 */
	public  void deleteTaskAll() throws DbException{
		db.deleteAll(Task.class);
	}
	
	/**
	 * 获取系统新消息数量
	 * @param <T>
	 * @throws DbException 
	 */
	
	public  long getNewTaskNum() throws DbException{
		if(isExit()){
			long num=db.count(Selector.from(Task.class).where("isNew","=","1"));
			return num;
		}else{
			return 0;
		}
	}
	
	/**
	 * 判别是否存在
	 * @return
	 * @throws DbException
	 */
	public boolean isExit() throws DbException{
		boolean istable= db.tableIsExist(Task.class);
		if(istable){
			long a=db.count(Task.class);
			if(a>0){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
}
